Introduction to RStudio
Day 1 - Introduction to Data Analysis with R
0.1 Difference between R and RStudio
R is the programming language and the program that does the actual work
- Can be used with many different programming environments
RStudio is the integrated development environment (IDE)
- Provides an interface to R
- Specifically built around R code
- Execute code
- Syntax highlighting
- File and project management
- …
0.2 Difference between R and RStudio
. . .
You can use R without RStudio but RStudio without R would be of little use
1 A quick tour around RStudio
1.1 A quick tour around RStudio
1.2 Console pane
Execute R code
Output from R code in scripts is printed there
Type a command into the console and execute with
Enter/Return
Use arrow keys to bring back last commands
1.3 Script pane
Write scripts with R code
Scripts are text files with R commands (file ending
.R)Use scripts to save commands for reuse
1.4 Script pane
- Create a new R script:
File -> New File -> R Script - Save an R script:
File->Save (Ctrl/Cmd + S) - Run code line by line with Run button (Ctrl+Enter/Cmd+Return)
- You can open multiple scripts
Use scripts for all your analysis and for commands that you want to save.
Use console for temporary commands, e.g. to test something.
1.5 Environment pane
Shows objects currently present in the R session
Is empty if you start R
1.6 Files pane
Similar to Explorer/Finder
Browse project structure and files
- Find and open files
- Create new folders
- Delete files
- Rename files
- …
Practical if you don’t want to switch between File Explorer and RStudio all the time
1.7 Plot pane
- Plots that are created with R will be shown here
2 Project oriented workflow
How to use RStudio to organize your projects
2.1 Project oriented workflow
- One directory with all files relevant for project
- Scripts, data, plots, documents, …
MyProject
|
|- data
|
|- docs
| |
| |- notes
| |
| |- reports
|
|- R
| |
| |- clean_data.R
| |
| |- statistics.R
|
|Example project structure
2.2 Project oriented workflow
- One directory with all files relevant for project
- Scripts, data, plots, documents, …
- An RStudio project is just a normal directory with an .Rproj file
MyProject
|
|- data
|
|- docs
| |
| |- notes
| |
| |- reports
|
|- R
| |
| |- clean_data.R
| |
| |- statistics.R
|
|- MyProject.RProjExample RStudio project structure
2.3 Project oriented workflow
Advantages of using RStudio projects
- Easy to navigate in R Studio (File pane)
- Easy to find and access scripts and data in RStudio
- Project root is working directory
- Open multiple projects simultaneously in separate RStudio instances
MyProject
|
|- data
|
|- docs
| |
| |- notes
| |
| |- reports
|
|- R
| |
| |- clean_data.R
| |
| |- statistics.R
|
|- MyProject.RProjExample RStudio project structure
2.4 Create an RStudio project
Create a project from scratch:
- File -> New Project -> New Directory -> New Project
- Enter a directory name (this will be the name of your project)
- Choose the Directory where the project should be initiated
- Create Project
RStudio will now create and open the project for you.
2.6 Open a project from outside RStudio
To open an RStudio project from your file explorer/finder, just double click on the .Rproj file
2.7 Open a project inside RStudio
To open an RStudio project from RStudio, click on the project symbol on the top right of R Studio and select the project from the list.
2.8 A tip before we get started
Learn the most important keyboard shortcuts of R Studio.
. . .
Find all shortcuts under Tools -> Keyboard Shortcuts Help
- Save active file: Ctrl/Cmd + S
- Run current line: Ctrl/Cmd + Enter
- Create new R Script: Ctrl/Cmd + N
- Undo: Ctrl/Cmd + Z
- Redo: Ctrl/Cmd + Y
- Copy/Paste: Ctrl/Cmd + C/V
3 Now you
Task 1 (15 min)
Set up your own RStudio project for this workshop
Find the task description here